/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-07-01 12:03:08
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-07-01 12:03:13
 * @FilePath: \keep_motion_api\models\product\productCategory.model.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-06-30 16:47:44
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-06-30 16:48:06
 * @FilePath: \keep_motion_api\models\product.model.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// 产品分类模型
const productCategorySchema = new Schema({
  name: { type: String, required: true },
  parent_id: { 
    type: Schema.Types.ObjectId, 
    ref: 'ProductCategory' 
  },
  level: Number,
  icon: String
});
productCategorySchema.statics.getCategories = async function (parent_id, offset, limit) {
  try {
    const query = {};
    if (parent_id) {
      query.parent_id = parent_id;
    }
    const total = await this.countDocuments(query);
    const rows = await this.find(query)
        .skip(Number(offset))
        .limit(Number(limit));
    return { rows, total };
  } catch (error) {
    throw error;
  } 
}
productCategorySchema.statics.createCategory = async function (data) {
  try {
    const category = new this(data);
    return await category.save();
  } catch (error) {
    throw error;
  }
}
productCategorySchema.statics.updateCategory = async function (id, data) {
  try {
    return await this.findByIdAndUpdate(id, data, { new: true });
  } catch (error) {
    throw error;
  }
}
productCategorySchema.statics.deleteCategory = async function (id) {
  try {
    return await this.findByIdAndDelete(id);
  } catch (error) {
    throw error;
  }
}
productCategorySchema.statics.updateOrder = async function (id, data) {
  try {
    return await this.findByIdAndUpdate(id, data, { new: true });
  } catch (error) {
    throw error;
  }
}
module.exports = mongoose.model('ProductCategory', productCategorySchema);
